/**
 * Copyright (C), 2020-2021 by colley.ma
 * File Name: SwakAutoConfiguration.java
 * Encoding: UTF-8
 * Date: 2021年3月31日 上午11:25:08
 * History:
*/
package com.swak.frame.spring.configuration;

import java.util.concurrent.ThreadPoolExecutor;

import javax.validation.Validator;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.swak.frame.cache.parser.SwakExtCacheAnnotationParser;
import com.swak.frame.common.SpringBeanFactory;
import com.swak.frame.eventbus.EventBusAdapter;
import com.swak.frame.job.SwakTaskJobRegister;
import com.swak.frame.monitor.async.ContextCopyingDecorator;
import com.swak.frame.system.SystemConfigurable;
import com.swak.frame.util.ValidatorUtils;

@Configuration
@ComponentScan(basePackages = "com.swak.frame")
@EnableAspectJAutoProxy
@Order(Ordered.HIGHEST_PRECEDENCE + 1000)
public class SwakAutoConfiguration {

	@Bean
	@ConditionalOnMissingBean(SpringBeanFactory.class)
	public SpringBeanFactory springBeanFactory() {
		return new SpringBeanFactory();
	}

	@Bean
	public CommonsMultipartResolver multipartResolver() {
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
		multipartResolver.setDefaultEncoding("UTF-8");
		multipartResolver.setMaxUploadSize(31 * 1024 * 1024);
		multipartResolver.setMaxInMemorySize(40960);
		return multipartResolver;
	}

	/**
	 * 单独的线程池 1,10,100000，超过丢弃
	 * @return
	 */
	@Bean
	public EventBusAdapter eventBusAdapter() {
		ThreadPoolTaskExecutor eventBusPool = new ThreadPoolTaskExecutor();
		eventBusPool.setCorePoolSize(1);// 当前线程数
		eventBusPool.setMaxPoolSize(10);// 最大线程数
		eventBusPool.setQueueCapacity(10000);
		eventBusPool.setWaitForTasksToCompleteOnShutdown(true);
		eventBusPool.setAwaitTerminationSeconds(60 * 10);// 等待时间 （默认为0，此时立即停止），并没等待xx秒后强制停止
		eventBusPool.setThreadNamePrefix("EventBus-threadpool-");
		eventBusPool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
		eventBusPool.setTaskDecorator(new ContextCopyingDecorator());
		eventBusPool.initialize(); // 初始化
		EventBusAdapter eventBusAdapter = new EventBusAdapter(eventBusPool);
		return eventBusAdapter;
	}

	@Bean
	public Validator validator() {
		return ValidatorUtils.getValidator();
	}

	@Bean
	public SwakTaskJobRegister swakTaskRegister(final SystemConfigurable systemConfig) {
		SwakTaskJobRegister swakTaskRegister = new SwakTaskJobRegister();
		swakTaskRegister.setSystemConfig(systemConfig);
		return swakTaskRegister;
	}

	@Bean
	public SwakExtCacheAnnotationParser extCacheAnnotationParser() {
		SwakExtCacheAnnotationParser extCacheAnnotationParser = new SwakExtCacheAnnotationParser();
		return extCacheAnnotationParser;
	}
}
